/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlaylistCellWithArtPanel.java
 *
 * Created on Mar 8, 2009, 2:36:51 PM
 */
package com.bff.mpd.musicjukebox.ui.components;

import com.bff.bjj.ui.component.BJJResizeListPanel;
import com.bff.mpd.musicjukebox.lib.ColorProperties;
import com.bff.mpd.musicjukebox.lib.JukeboxProperties;
import com.bff.mpd.musicjukebox.lib.actions.PlaylistDownAction;
import com.bff.mpd.musicjukebox.lib.actions.PlaylistUpAction;
import com.bff.mpd.utils.Utils;
import java.awt.Color;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JList;
import org.bff.javampd.MPDPlaylist;
import org.bff.javampd.objects.MPDSong;

/**
 *
 * @author Music
 */
public class PlaylistCellPanel extends BJJResizeListPanel {

    private JButton btnPlaylistUp;
    private JButton btnPlaylistDown;
    private Action playlistUpAction;
    private Action playlistDownAction;
    private MPDPlaylist playlist;
    private JList listPlaylist;
    private MPDSong item;
    private int artistLabelSize;
    private int albumLabelSize;
    private int titleLabelSize;
    private static final String LABEL_NULL_TITLE = Utils.getMessage("LABEL_NULL_TITLE");
    private static final int MAX_LABEL_SIZE_TITLE = 999;
    private static final int MAX_LABEL_SIZE_ALBUM = 999;
    private static final int MAX_LABEL_SIZE_ARTIST = 999;
    public static final int PERCENT_TITLE = 20;
    public static final int PERCENT_ALBUM = 30;
    public static final int PERCENT_ARTIST = 25;
    public static final int PERCENT_TOTAL = 9;
    private static final int DEFAULT_WIDTH = 400;
    private static int currentListSize;

    /** Creates new form PlaylistCellWithArtPanel */
    public PlaylistCellPanel(MPDSong object, JList listPlaylist) {
        super(listPlaylist);
        initComponents();
        this.item = object;
        this.listPlaylist = listPlaylist;

        this.playlist = JukeboxProperties.getInstance().getPlaylist();

        artistLabelSize = MAX_LABEL_SIZE_ARTIST;
        albumLabelSize = MAX_LABEL_SIZE_ALBUM;
        titleLabelSize = MAX_LABEL_SIZE_TITLE;

        if (object.getTitle() == null) {
            if (JukeboxProperties.getInstance().isUseFileNameForUnknown()) {
                object.setTitle(object.getFile());
            } else {
                object.setTitle(LABEL_NULL_TITLE);
            }
        }

        //we dont want to replace the list size if it has been set
        if (currentListSize == 0) {
            currentListSize = DEFAULT_WIDTH;
        }

        createLabels();

        playlistUpAction = new PlaylistUpAction(listPlaylist, getPlaylist());
        playlistDownAction = new PlaylistDownAction(listPlaylist, getPlaylist());

        btnPlaylistUp = new javax.swing.JButton();
        btnPlaylistUp.setContentAreaFilled(false);
        btnPlaylistDown = new javax.swing.JButton();
        btnPlaylistDown.setContentAreaFilled(false);

        btnPlaylistUp.setBackground(java.awt.Color.lightGray);
        btnPlaylistUp.setBorder(null);
        btnPlaylistUp.setBorderPainted(false);
        btnPlaylistUp.setFocusPainted(false);
        btnPlaylistUp.setIconTextGap(0);
        btnPlaylistUp.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnPlaylistUp.setMaximumSize(new java.awt.Dimension(50, 50));
        btnPlaylistUp.setMinimumSize(new java.awt.Dimension(16, 16));
        btnPlaylistUp.setPreferredSize(new java.awt.Dimension(16, 16));

        btnPlaylistDown.setBorder(null);
        btnPlaylistDown.setBorderPainted(false);
        btnPlaylistDown.setIconTextGap(0);
        btnPlaylistDown.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnPlaylistDown.setMaximumSize(new java.awt.Dimension(50, 50));
        btnPlaylistDown.setMinimumSize(new java.awt.Dimension(16, 16));
        btnPlaylistDown.setPreferredSize(new java.awt.Dimension(16, 16));

        btnPlaylistUp.setAction(playlistUpAction);
        btnPlaylistDown.setAction(playlistDownAction);
        btnPlaylistDown.setText(null);
        btnPlaylistUp.setText(null);
        lblNowPlaying.setVisible(false);

        btnPanel.setBackground(ColorProperties.LIST_BACKGROUND_SELECTED);
        btnPanel.add(btnPlaylistUp);
        btnPanel.add(btnPlaylistDown);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        detailPanel = new javax.swing.JPanel();
        lblNowPlaying = new javax.swing.JLabel();
        lblTitle = new javax.swing.JLabel();
        lblFrom = new javax.swing.JLabel();
        lblAlbum = new javax.swing.JLabel();
        lblBy = new javax.swing.JLabel();
        lblArtist = new javax.swing.JLabel();
        lblNowPlaying2 = new javax.swing.JLabel();
        btnPanel = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        lblNowPlaying.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/slimserver/musicnote_small.png"))); // NOI18N
        detailPanel.add(lblNowPlaying);

        lblTitle.setText("jLabel1");
        detailPanel.add(lblTitle);

        lblFrom.setForeground(java.awt.Color.gray);
        lblFrom.setText(Utils.getMessage("LABEL_FROM")); // NOI18N
        detailPanel.add(lblFrom);

        lblAlbum.setText("jLabel2");
        detailPanel.add(lblAlbum);

        lblBy.setForeground(java.awt.Color.gray);
        lblBy.setText(Utils.getMessage("LABEL_BY")); // NOI18N
        detailPanel.add(lblBy);

        lblArtist.setText("jLabel3");
        detailPanel.add(lblArtist);

        lblNowPlaying2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/slimserver/musicnote_small.png"))); // NOI18N
        detailPanel.add(lblNowPlaying2);

        add(detailPanel, java.awt.BorderLayout.WEST);
        add(btnPanel, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel btnPanel;
    private javax.swing.JPanel detailPanel;
    private javax.swing.JLabel lblAlbum;
    private javax.swing.JLabel lblArtist;
    private javax.swing.JLabel lblBy;
    private javax.swing.JLabel lblFrom;
    private javax.swing.JLabel lblNowPlaying;
    private javax.swing.JLabel lblNowPlaying2;
    private javax.swing.JLabel lblTitle;
    // End of variables declaration//GEN-END:variables

    public void setEven() {
        setBackground(ColorProperties.LIST_BACKGROUND_EVEN);
    }

    public void setOdd() {
        setBackground(ColorProperties.LIST_BACKGROUND_ODD);
    }

    public void setCurrentPlaying() {
        lblNowPlaying.setVisible(true);
        lblNowPlaying2.setVisible(true);
    }

    public void setNotPlaying() {
        lblNowPlaying.setVisible(false);
        lblNowPlaying2.setVisible(false);
    }

    public void setSelected() {
        setForeground(ColorProperties.LIST_FOREGROUND_SELECTED);
        setBackground(ColorProperties.LIST_BACKGROUND_SELECTED);
        lblFrom.setForeground(ColorProperties.LIST_FOREGROUND_SELECTED);
        lblBy.setForeground(ColorProperties.LIST_FOREGROUND_SELECTED);
        lblFrom.setBackground(ColorProperties.LIST_BACKGROUND_SELECTED);
        lblBy.setBackground(ColorProperties.LIST_BACKGROUND_SELECTED);
        setButtonsVisible(true);
    }

    public void setNotSelected() {
        setForeground(ColorProperties.LIST_FOREGROUND);
        lblFrom.setForeground(Color.GRAY);
        lblBy.setForeground(Color.GRAY);

        setButtonsVisible(false);
        //setBackground(ColorProperties.LIST_BACKGROUND);
    }

    @Override
    public void setBackground(Color bg) {
        super.setBackground(bg);
        //detailPanel.setBackground(bg);
        if (detailPanel != null) {
            detailPanel.setBackground(bg);
        }
    }

    @Override
    public void setForeground(Color fg) {
        super.setForeground(fg);
        if (detailPanel != null) {
            detailPanel.setForeground(fg);
        }
        setLabelColors(fg);
    }

    private void setLabelColors(Color color) {
        if (lblTitle != null) {
            lblTitle.setForeground(color);
            lblArtist.setForeground(color);
            lblAlbum.setForeground(color);
        }
    }

    public MPDPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(MPDPlaylist playlist) {
        this.playlist = playlist;
    }

    public void setUpAction(boolean enabled) {
        playlistUpAction.setEnabled(enabled);
    }

    public void setDownAction(boolean enabled) {
        playlistDownAction.setEnabled(enabled);
    }

    private void setButtonsVisible(boolean visible) {
        btnPanel.setVisible(visible);
    }

    public void eventReceived(java.awt.event.MouseEvent evt) {

        int index = listPlaylist.locationToIndex(evt.getPoint());

        if (index < 1) {
            btnPlaylistUp.setEnabled(false);
        } else {
            btnPlaylistUp.setEnabled(true);
        }

        if (index >= listPlaylist.getModel().getSize() - 1) {
            btnPlaylistDown.setEnabled(false);
        } else {
            btnPlaylistDown.setEnabled(true);
        }

        listPlaylist.repaint();

    }

    public void setCurrent() {
        setLabelColors(ColorProperties.PLAYLIST_CURRENT_PLAYING_FOREGROUND);
    }

    public void setNotCurrent() {
        lblNowPlaying.setVisible(false);
        setLabelColors(ColorProperties.LIST_FOREGROUND_SELECTED);
    }

    /**
     * @return the item
     */
    public MPDSong getItem() {
        return item;
    }

    /**
     * @return the artistLabelSize
     */
    public int getArtistLabelSize() {
        return artistLabelSize;
    }

    /**
     * @param artistLabelSize the artistLabelSize to set
     */
    public void setArtistLabelSize(int artistLabelSize) {
        this.artistLabelSize = artistLabelSize;
    }

    /**
     * @return the albumLabelSize
     */
    public int getAlbumLabelSize() {
        return albumLabelSize;
    }

    /**
     * @param albumLabelSize the albumLabelSize to set
     */
    public void setAlbumLabelSize(int albumLabelSize) {
        this.albumLabelSize = albumLabelSize;
    }

    /**
     * @return the titleLabelSize
     */
    public int getTitleLabelSize() {
        return titleLabelSize;
    }

    /**
     * @param titleLabelSize the titleLabelSize to set
     */
    public void setTitleLabelSize(int titleLabelSize) {
        this.titleLabelSize = titleLabelSize;
    }

    private void createLabels() {
        int total = getCurrentListSize() / PERCENT_TOTAL;

        String title = getItem().getTitle();
        String album = getItem().getAlbum() == null ? "" : getItem().getAlbum().getName();

        boolean albumEmpty = false;
        boolean artistEmpty = false;

        if ("".equalsIgnoreCase(album)
                || getItem().getAlbum().getName() == null) {
            albumEmpty = true;
        }

        String artist = "";
        try {
            artist = getItem().getArtist().getName() == null ? "" : getItem().getArtist().getName();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if ("".equalsIgnoreCase(artist)) {
            artistEmpty = true;
        }

        int totalChars = title.length()
                + album.length()
                + artist.length();

        if (totalChars >= total) {
            if (artistEmpty) {
                setArtistLabelSize(0);
            } else {
                setArtistLabelSize(getCurrentListSize() / PERCENT_ARTIST);
            }

            if (albumEmpty) {
                setAlbumLabelSize(0);
            } else {
                setAlbumLabelSize(getCurrentListSize() / PERCENT_ALBUM);
            }
            try {
                artist = artist.length() < getArtistLabelSize() ? artist : artist.substring(0, getArtistLabelSize()) + "...";

                totalChars = title.length()
                        + album.length()
                        + artist.length() - 3;

                if (totalChars >= total) {
                    album = album.length() < getAlbumLabelSize() ? album : album.substring(0, getAlbumLabelSize()) + "...";
                }

                totalChars = title.length()
                        + album.length() - 3
                        + artist.length() - 3;

                if (totalChars >= total) {
                    setTitleLabelSize(total - (getArtistLabelSize() + getAlbumLabelSize()));
                    title = title.length() < getTitleLabelSize() ? title : title.substring(0, getTitleLabelSize()) + "...";
                }

            } catch (Exception ex) {
                Logger.getLogger(PlaylistCellPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        lblTitle.setText(title + " ");
        if (albumEmpty) {
            detailPanel.remove(lblFrom);
            detailPanel.remove(lblAlbum);
        } else {
            lblAlbum.setText(album + " ");
        }

        if (artistEmpty) {
            detailPanel.remove(lblBy);
            detailPanel.remove(lblArtist);
        } else {
            lblArtist.setText(artist + " ");
        }
        revalidate();
        repaint();
    }

    @Override
    public void adjustComponent(Dimension size) {
        setCurrentListSize((int) size.getWidth());
        createLabels();
    }

    /**
     * @return the currentListSize
     */
    private static int getCurrentListSize() {
        return currentListSize;
    }

    /**
     * @param aCurrentListSize the currentListSize to set
     */
    private static void setCurrentListSize(int aCurrentListSize) {
        currentListSize = aCurrentListSize;
    }
}
